package com.itsu.spbspider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itsu.spbspider.entity.User;
import com.itsu.spbspider.exception.SystemException;
import com.itsu.spbspider.mapper.UserMapper;
import com.itsu.spbspider.service.UserService;
import com.itsu.spbspider.util.EncryptUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class UserSeriveImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User login(User user) throws SystemException {
        try {
            if (user == null || StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword()))
                return null;
            QueryWrapper<User> condition = new QueryWrapper<>();
            condition.eq("username", user.getUsername());
            condition.eq("password", DigestUtils.md5Hex(user.getPassword()));
            User u = userMapper.selectOne(condition);
            return u;
        } catch (Exception e) {
            throw new SystemException(e);
        }
    }

    @Override
    public User loginWithCookie(User user) throws SystemException {
        try {
            if (user == null || StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword()))
                return null;
            QueryWrapper<User> condition = new QueryWrapper<>();
            condition.eq("username", EncryptUtil.decode(user.getUsername()));
            condition.eq("password", user.getPassword());
            User u = userMapper.selectOne(condition);
            return u;
        } catch (Exception e) {
            throw new SystemException(e);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void resetPwd(Map params) throws Exception {
        try {
            String username = (String) params.get("username");
            String newpwd = (String) params.get("newpwd");
            String renewpwd = (String) params.get("renewpwd");
            String oldpwd = (String) params.get("oldpwd");
            if (StringUtils.isBlank(username) || StringUtils.isBlank(oldpwd)) {
                throw new SystemException("username / old password is required");
            }

            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.eq("username", username);
            qw.eq("password", DigestUtils.md5Hex(oldpwd));
            if (userMapper.selectCount(qw) <=0) {
                throw new SystemException("原密码不匹配");
            }

            if (StringUtils.compare(newpwd, renewpwd) != 0) {
                throw new SystemException("两次密码输入不一致");
            }


            int update = userMapper.updatePwd(username, DigestUtils.md5Hex(newpwd));
            if (update <= 0) {
                throw new SystemException("修改失败");
            }
        } catch (Exception e) {
            throw new SystemException(e);
        }
    }
}
